package com.lyc;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.lyc.contant.FaceRegisterConfig;
import com.lyc.pojo.FaceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.lyc.register.CacheRegisterFace.faceCache;

/**
 * @author Administrator
 * @date 2024/4/26
 * @project hushan-iot-platform
 * @package com.lyc
 */
@Component
public class ApplicationRunListener implements ApplicationRunner {

    @Autowired
    FaceRegisterConfig faceRegisterConfig;
    Logger logger = LoggerFactory.getLogger(ApplicationRunListener.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String filePath = faceRegisterConfig.getFile_path();
        if (FileUtil.exist(filePath)) {
            List<String> lines = FileUtil.readUtf8Lines(filePath, new ArrayList<>());
            for (String line : lines) {
                FaceModel faceModel = JSONUtil.toBean(line, FaceModel.class);
                faceCache.put(faceModel.getId(), faceModel);
            }
            logger.info("人脸数据加载完成，共加载{}条数据", lines.size());
        }
    }
}
